package com.david.zhufengfm.Fragments.discover;


import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.david.zhufengfm.Constants;
import com.david.zhufengfm.Fragments.BaseFragment;
import com.david.zhufengfm.R;
import com.david.zhufengfm.tasks.DiscoverCategoryTask;
import com.david.zhufengfm.tasks.TaskCallback;
import com.david.zhufengfm.tasks.TaskResult;

import java.util.ArrayList;
import java.util.List;

import static com.david.zhufengfm.R.id.text_list;

/**
 * A simple {@link Fragment} subclass.
 */
public class DiscoverCategoryFragment extends BaseFragment implements TaskCallback {


    public DiscoverCategoryFragment() {
        // Required empty public constructor
    }

    //用于一次初始化的东西放在这里
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DiscoverCategoryTask task=new DiscoverCategoryTask(this);
        task.execute();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View ret = inflater.inflate(R.layout.fragment_discover_category, container, false);
        ListView listView= (ListView) ret.findViewById(text_list);
        ArrayList<String>data=new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            data.add("test--"+i);
        }
        ArrayAdapter<String>adapter=new ArrayAdapter<String>(
                getContext(),android.R.layout.simple_list_item_1,data);

        listView.setAdapter(adapter);
        return ret;
    }


    @Override
    public String getFragmentTitle() {
        return "分类";
    }

    @Override
    public void onTaskFinished(TaskResult result) {
        if (result != null) {
            int action = result.action;
            if (action== Constants.TASK_ACTION_DISCOVER_CATEGORIES){
                //TODO 结果从发现-分类 任务中返回的，获取的是分类
                Log.d("Category_Code",""+result.resultCode);
                if (result.resultCode == Constants.TASK_RESULT_OK) {
                    //TODO 加载成功
                    Object data = result.data;
                    if (data != null&&data instanceof List) {
                        List list= (List) data;
                        Log.d("Category","List="+list);
                    }
                }else{
                    //TODO 加载失败
                    Log.d("Category","Error");
                }
            }
        }
    }
}
